﻿using Dapper;
using Entity;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Realize;

namespace WebApplication9.Controllers.Recruit_Manage.JL
{
    public class ER_YxSelectController : Controller//有效简历查询
    {

        private readonly IConfiguration configuration;
        private IWebHostEnvironment _environment;

        public ER_YxSelectController(IConfiguration configuration, IWebHostEnvironment environment)
        {
            this.configuration = configuration;
            _environment = environment;
        }
        public IActionResult Index()
        {
            return View();
        }

        [HttpGet("FirstLevelYxJlSelect")]
        public async Task<IActionResult> FirstLevelYxJlSelect()
        {
            using (var connection = new SqlConnection(configuration["SqlServer"]))
            {
                await connection.OpenAsync();

                // 查询I级机构
                var firstKinds = await connection.QueryAsync<dynamic>(
                    "SELECT major_kind_id AS id, major_kind_name AS name FROM config_major_kind");

                // 构建级联数据结构
                var result = new List<dynamic>();

                foreach (var firstKind in firstKinds)
                {
                    // 查询对应的II级机构
                    var secondKinds = await connection.QueryAsync<dynamic>(
                        "SELECT major_id AS id, major_name AS name " +
                        "FROM config_major " +
                        "WHERE major_kind_id = @Id",
                        new { Id = firstKind.id });

                    var secondList = new List<dynamic>();

                    foreach (var secondKind in secondKinds)
                    {
                        // 查询对应的III级机构
                        var thirdKinds = await connection.QueryAsync<dynamic>(
                            "SELECT third_kind_id AS id, third_kind_name AS name " +
                            "FROM config_file_third_kind " +
                            "WHERE second_kind_id = @Id",
                            new { Id = secondKind.id });

                        secondList.Add(new
                        {
                            secondKind.id,
                            secondKind.name,
                            children = thirdKinds
                        });
                    }

                    result.Add(new
                    {
                        firstKind.id,
                        firstKind.name,
                        children = secondList
                    });
                }

                return Ok(result);
            }
        }

        [HttpGet]
        public async Task<ActionResult> SelectByYxTj_ER(int CurrentPage, int PageSize, string id1, string id2, string Tj, string ksTime, string jsTime)
        {
            IER eR = new ER();
            List<Engage_Resume> zhi = await eR.selectYxJj(id1, id2, Tj, ksTime, jsTime, configuration["SqlServer"]);
            List<Engage_Resume> zhi2 = zhi.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();
            PagedViewModel<Engage_Resume> cc = new PagedViewModel<Engage_Resume>() { itemas = zhi2, total = zhi.Count };
            return Ok(new
            {
                list = cc
            });
        }

        public IActionResult Select()
        {
            return View();
        }

    }
}
